home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 12 / Cream of the Crop 12 (Part II) / Cream of the Crop 12 (Part II).iso / OS2 / V15N04.ZIP / WARPCA.ZIP / WCABSRC.ZIP / ASSOCIAT.CPP next >
Encoding:
C/C++ Source or Header  |  1996-01-18  |  6.6 KB  |  257 lines

  1. // DAssociationsDlg -- Class implementation
  2. #include <cstring.h>
  3. #include <dir.h>
  4. #include <string.h>
  5. #include <stdio.h>
  6. #include <classlib\arrays.h>
  7. #include <owl\owlpch.h>
  8. #include <owl\dialog.h>
  9. #include <owl\edit.h>
  10. #include <owl\listbox.h>
  11. #include <owl\static.h>
  12. #include <owl\button.h>
  13. #include "resource.h"
  14. #include "os2api.h"
  15. #include "fileasoc.h"
  16. #include "associat.h"
  17.  
  18. #define DID_OK      1
  19. #define DID_CANCEL  2
  20.  
  21. DEFINE_RESPONSE_TABLE1(DAssociationsDlg, TDialog)
  22.     EV_CHILD_NOTIFY(IDC_ASSOC_LIST, LBN_SELCHANGE, OnAssocListClick),
  23.     EV_CHILD_NOTIFY(IDC_DELETE_ASSOC, BN_CLICKED, CmDeleteAssoc),
  24.     EV_CHILD_NOTIFY(DID_OK, BN_CLICKED, CmOk),
  25.     EV_CHILD_NOTIFY(DID_CANCEL, BN_CLICKED, CmCancel),
  26. END_RESPONSE_TABLE;
  27.  
  28. DAssociationsDlg::DAssociationsDlg(TWindow *parent, const TISArrayAsVector <FILEASSOCIATION> *pFileAssociations,
  29.         const LPSTR lpszNewExtension) : TDialog(parent, IDD_ASSOCIATE_DLG, 0)
  30. {
  31.     nCurrentEditIndex = -1;
  32.     bAssocDeleted = FALSE;
  33.  
  34.     // Save pointer, and copy original data for editing.
  35.     if(!pFileAssociations)
  36.     {
  37.         MessageBox("Bad pointer in DAssociationsDlg::DAssociationsDlg!", "Error", MB_ICONSTOP);
  38.         return;
  39.     }
  40.  
  41.     pOldFileAssociations = (TISArrayAsVector <FILEASSOCIATION> *)pFileAssociations;
  42.     pNewFileAssociations = new TISArrayAsVector <FILEASSOCIATION>(10, 0, 1);
  43.  
  44.     FILEASSOCIATION *pOldAssoc;
  45.     FILEASSOCIATION *pNewAssoc;
  46.  
  47.     // Copy old assocation table array.
  48.     for(int i = 0; i < pFileAssociations->GetItemsInContainer(); i++)
  49.     {
  50.         pOldAssoc = (*pFileAssociations)[i];
  51.         if(!pOldAssoc)
  52.             continue;
  53.         pNewAssoc = new FILEASSOCIATION(*pOldAssoc);
  54.         pNewFileAssociations->Add(pNewAssoc);
  55.     }
  56.     if(lpszNewExtension)
  57.         strcpy(szInitExt, lpszNewExtension);
  58.     else
  59.         szInitExt[0] = 0;
  60. }
  61.  
  62. void DAssociationsDlg::SetupWindow()
  63. {
  64.     TDialog::SetupWindow();
  65.  
  66.     TListBox AssocLB(this, IDC_ASSOC_LIST);
  67.     AssocLB.Attr.Style |= LBS_SORT; // Set sort bit.
  68.  
  69.     // Load current associations into our listbox. [Ext. + ( Desc, )]
  70.     string strEntry;
  71.     string strTemp;
  72.  
  73.     SetDlgItemText(IDC_EXT, szInitExt);
  74.  
  75.     FILEASSOCIATION *pAssoc;
  76.     int nNewIndex;
  77.     int nArrayIndex;
  78.     int nMatchIndex = -1;
  79.  
  80.     int nCount = pNewFileAssociations->GetItemsInContainer();
  81.     for(int i = -1; i < nCount; i++)
  82.     {
  83.         if(i == -1)  // First time, just add 'none'.
  84.             strEntry = "(None)";
  85.         else
  86.         {
  87.             pAssoc = (*pNewFileAssociations)[i];
  88.             if(!pAssoc)
  89.                 continue;
  90.  
  91.             if(strlen(pAssoc->szExt) > 0)
  92.             {
  93.                 strTemp = pAssoc->szExt;
  94.                 strTemp.to_upper();
  95.             }
  96.             else
  97.                 strTemp = "";
  98.  
  99.             strEntry = strTemp;
  100.             if(strlen(pAssoc->szDesc) > 0)
  101.             {
  102.                 strEntry += " (";
  103.                 strEntry += pAssoc->szDesc;
  104.                 strEntry += ")";
  105.             }
  106.             else
  107.                 strEntry += "(N.A.)";
  108.         }
  109.         nNewIndex = SendDlgItemMessage(IDC_ASSOC_LIST, LB_ADDSTRING, -1, (LPARAM)(LPSTR)strEntry.c_str());
  110.         if(nNewIndex != -1)
  111.         {
  112.             nArrayIndex = i > -1 ? i : -1;  // No array index for "None".
  113.             // Store index to file association array along with list item.
  114.             SendDlgItemMessage(IDC_ASSOC_LIST, LB_SETITEMDATA, nNewIndex, (LPARAM)nArrayIndex);
  115.             // Save matching index, if this matches.
  116.             if(nArrayIndex != -1)
  117.                 if(strcmpi(pAssoc->szExt, szInitExt) == 0)
  118.                     nMatchIndex = nNewIndex;
  119.         }
  120.     }
  121.     if(nMatchIndex != -1)
  122.     {
  123.         SendDlgItemMessage(IDC_ASSOC_LIST, LB_SETCURSEL, nMatchIndex, 1);
  124.         nCurrentEditIndex = nMatchIndex;
  125.     }
  126.     else
  127.     {
  128.         // 'None'.
  129.         SendDlgItemMessage(IDC_ASSOC_LIST, LB_SETCURSEL, 0, 1);
  130.         nCurrentEditIndex = 0;
  131.     }
  132. }
  133.  
  134. void DAssociationsDlg::CmOk()
  135. {
  136.     // Retrieve new association settings and write them back to parent's data.
  137.  
  138.     // Now update new extension.
  139.     int nSel = SendDlgItemMessage(IDC_ASSOC_LIST, LB_GETCURSEL, 0, 0);
  140.     FILEASSOCIATION TempAssoc;
  141.     FILEASSOCIATION *pThisFileAssoc;
  142.     FILEASSOCIATION *pAssoc;
  143.     int nIndex;
  144.     char szTemp[256];
  145.  
  146.     if(nCurrentEditIndex == -1 && bAssocDeleted == FALSE)
  147.         CloseWindow(FALSE);
  148.  
  149.     // Get new settings.
  150.     GetDlgItemText(IDC_EXT, TempAssoc.szExt, 4);
  151.     GetDlgItemText(IDC_ASSOC_DESC, TempAssoc.szDesc, 128);
  152.     GetDlgItemText(IDC_ASSOC_PATH, TempAssoc.szEXEPathName, 256);
  153.  
  154.     if(strlen(TempAssoc.szExt) > 0)
  155.     {
  156.         if(strlen(TempAssoc.szDesc) == 0)
  157.         {
  158.             MessageBox("Fill-in a description first!", "Description Missing", MB_ICONEXCLAMATION);
  159.             return;
  160.         }
  161.  
  162.         if(strlen(TempAssoc.szEXEPathName) == 0)
  163.         {
  164.             MessageBox("Fill-in a valid path first!", "Path Missing", MB_ICONEXCLAMATION);
  165.             return;
  166.         }
  167.     }
  168.  
  169.     // Copy all entries to global array.
  170.     pOldFileAssociations->Flush(TShouldDelete::Delete);
  171.     int nCount = SendDlgItemMessage(IDC_ASSOC_LIST, LB_GETCOUNT, 0, 0);
  172.     for(int i = 0; i < nCount; i++)
  173.     {
  174.         // Copy each association from listbox.
  175.         if((i == -1 || i == nSel) && strlen(TempAssoc.szExt) > 0)
  176.             // New entry or edited entry.
  177.             pOldFileAssociations->Add(new FILEASSOCIATION(TempAssoc));
  178.         else
  179.         {
  180.             nIndex = SendDlgItemMessage(IDC_ASSOC_LIST, LB_GETITEMDATA, i, 0);            if(nIndex != -1)
  181.             {
  182.                 pAssoc = (*pNewFileAssociations)[nIndex];
  183.                 if(pAssoc)
  184.                     pOldFileAssociations->Add(new FILEASSOCIATION(*pAssoc));
  185.             }
  186.         }
  187.     }
  188.  
  189.     pNewFileAssociations->Flush(TShouldDelete::Delete);
  190.  
  191.     delete pNewFileAssociations;
  192.     pNewFileAssociations = NULL;
  193.  
  194.     CloseWindow(TRUE);
  195. }
  196.  
  197. void DAssociationsDlg::CmCancel()
  198. {
  199.     pNewFileAssociations->Flush(TShouldDelete::Delete);
  200.  
  201.     delete pNewFileAssociations;
  202.     pNewFileAssociations = NULL;
  203.  
  204.     CloseWindow(FALSE);
  205. }
  206.  
  207. void DAssociationsDlg::OnAssocListClick()
  208. {
  209.     // Fill edit controls with current association if one is selected.
  210.     int nIndex;
  211.     FILEASSOCIATION *pAssoc;
  212.     int nSel = SendDlgItemMessage(IDC_ASSOC_LIST, LB_GETCURSEL, 0, 0);
  213.     if(nSel > 0)
  214.     {
  215.         nIndex =(int)SendDlgItemMessage(IDC_ASSOC_LIST, LB_GETITEMDATA, nSel, 0);
  216.         if(nIndex > -1)
  217.         {
  218.             pAssoc = (FILEASSOCIATION *)(*pNewFileAssociations)[nIndex];
  219.             if(pAssoc)
  220.             {
  221.                 SetDlgItemText(IDC_EXT, pAssoc->szExt);
  222.                 SetDlgItemText(IDC_ASSOC_DESC, pAssoc->szDesc);
  223.                 SetDlgItemText(IDC_ASSOC_PATH, pAssoc->szEXEPathName);
  224.                 nCurrentEditIndex = nIndex;
  225.             }
  226.         }
  227.     }
  228.     else
  229.     {
  230.         // Note: 0 = 'None'
  231.         SetDlgItemText(IDC_EXT, "");
  232.         SetDlgItemText(IDC_ASSOC_DESC, "");
  233.         SetDlgItemText(IDC_ASSOC_PATH, "");
  234.         nIndex = -1;
  235.     }
  236. }
  237.  
  238. void DAssociationsDlg::CmDeleteAssoc()
  239. {
  240.     int nTotal;
  241.     // Deletes entry in LB if one is selected.
  242.     int nSel = SendDlgItemMessage(IDC_ASSOC_LIST, LB_GETCURSEL, 0, 0);
  243.     if(nSel > 0)  // Can't delete '(None)'--the first choice!
  244.     {
  245.         SendDlgItemMessage(IDC_ASSOC_LIST, LB_DELETESTRING, nSel, 0);
  246.         SendDlgItemMessage(IDC_ASSOC_LIST, LB_SETCURSEL, -1, 0);
  247.         bAssocDeleted = TRUE;
  248.  
  249.         SetDlgItemText(IDC_EXT, "");
  250.         SetDlgItemText(IDC_ASSOC_DESC, "");
  251.         SetDlgItemText(IDC_ASSOC_PATH, "");
  252.  
  253.         nCurrentEditIndex = -1;
  254.     }
  255. }
  256.  
  257.